<?php

namespace App\Http\Controllers\Api\SmallApp\Index;

use App\Http\Controllers\Common\ApiController;
use App\Models\Advert\Advert;
use App\Models\Article\ArticleInfo;
use App\Models\User\User;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class IndexController extends ApiController
{
    public function advert(Request $request) {

        $records = Advert::where('type',1)->where('status',1)->get(['pic_url']);
        return responseSuccess($records->toArray());
    }

    public function service(Request $request) {
        $records = ArticleInfo::where('id',1)->first(['title','content']);
        return responseSuccess($records->toArray());
    }

    public function userNotify(Request $request) {
        $records = ArticleInfo::where('id',2)->first(['title','content']);
        return responseSuccess($records->toArray());
    }

    public function finishWx(Request $request) {
        $token = $request->header('token');
        $id = checkJWT($token)['id'];

        Log::info('token:'.$token);
        Log::info('id:'.$id);
        Log::info('input:',$request->input());

        $wx_avatar = $request->input('wx_avatar');
        $wx_nickname = $request->input('wx_nickname');
        $code = $request->input('code');

        $this->validate($request, [
            'wx_avatar'=>'required',
            'wx_nickname'=>'required',
            'code'=>'required',
        ]);


        $config = config('wechat.default');
        $app = Factory::miniProgram($config);
        $res = $app->auth->session($code);

        Log::info('finishWx',compact('res'));


        $wx_samll_open_id = $res['openid']??'';

        User::where(compact('id'))->update(compact('wx_avatar','wx_nickname','wx_samll_open_id'));

        return responseSuccess();
    }

    public function updatePublicOpenId(Request $request) {

        //https://bjyoupei.techbz.cn/api/index/updatePublicOpenId
        Log::info('updatePublicOpenId',$request->input());
        $code = $request->input('code');
        $userId = $request->input('state');
        $user = User::where(['id'=>$userId])->first();
        $config = config('wechat.public');
        $app = Factory::officialAccount($config);

        $wxUser = $app->oauth->userFromCode($code);

        if(!empty($user) && isset($wxUser['token_response']['openid'])){
            $user->wx_public_open_id = $wxUser['token_response']['openid'];
            $user->save();
        }

       // Log::info('updatePublicOpenId',[$user->token_response->openid]);
        //Log::info('updatePublicOpenId',compact('user'));
        return responseSuccess();
    }

    public function getWxCode(Request $request) {

        //https://bjyoupei.techbz.cn/api/index/getWxCode
        return view('wxCode');
    }

    public function wxSmallMessage(Request $request) {

        return $_GET["echostr"];
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = 'bjyoupei';
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if ($tmpStr == $signature ) {
            return true;
        } else {
            return false;
        }
    }



}
